package ribbon.rule;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author: ash
 * @time: 2024/2/1  22:14:28
 * @description:
 */
public class MyRule extends AbstractLoadBalancerRule {

    @Override
    public Server choose(Object o) {
        ILoadBalancer loadBalancer = this.getLoadBalancer();
        //获取请求的当前实例
        List<Server> servers = loadBalancer.getReachableServers();

        int random = ThreadLocalRandom.current().nextInt(servers.size());

        Server server = servers.get(random);
        return server;
    }

    @Override
    public void initWithNiwsConfig(IClientConfig iClientConfig) {

    }
}
